﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using www.serviciipeweb.ro.iafblog.ExportDLL;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class Planning : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        PlanningData m_Data;
        public DataSet dsParamaters;
        
        #endregion
        public Planning()
        {
            InitializeComponent();
            this.Name = "计划报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent(); 
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new PlanningData(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void InitDataBinding()
        {
            try
            {
                this.setUI.Tables.Clear();

                setUI.AcceptChanges();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Yarn_Inventory_Click(object sender, RoutedEventArgs e)
        {
            this.Yarn_Inventory.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT CODE,GLLB,ART,VAR,BEZ,SUPPLIER,SUPPLIERBATCH,LOT,SUM(WEI) AS WEI "
                    + " FROM INVENTORY_YARN_REPORT_LB GROUP BY CODE,GLLB,ART,VAR,BEZ,SUPPLIER,SUPPLIERBATCH,LOT "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "YARN_INVENTORY";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["YARN_INVENTORY"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["YARN_INVENTORY"].Columns)
                    {
                        if (dc.ColumnName == "BEZ")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }
                #region TRANSLATE
                foreach (DataColumn dc in dsResult.Tables["YARN_INVENTORY"].Columns)
                {
                    if (dc.ColumnName == "CODE")
                    {
                        dc.ColumnName = "汇隆号";
                    }
                    if (dc.ColumnName == "ART")
                    {
                        dc.ColumnName = "纱线编号";
                    }
                    if (dc.ColumnName == "VAR")
                    {
                        dc.ColumnName = "变量";
                    }
                    if (dc.ColumnName == "BEZ")
                    {
                        dc.ColumnName = "纱线支数";
                    }
                    if (dc.ColumnName == "SUPPLIER")
                    {
                        dc.ColumnName = "供应商名称";
                    }
                    if (dc.ColumnName == "SUPPLIERBATCH")
                    {
                        dc.ColumnName = "供应商纺批";
                    }
                    if (dc.ColumnName == "LOT")
                    {
                        dc.ColumnName = "缸号";
                    }
                    if (dc.ColumnName == "WEI")
                    {
                        dc.ColumnName = "重量";
                    }
                }
                #endregion
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);

                }
                string filename = "纱线库存表" + System.DateTime.Today.ToShortDateString();
                string filepath = @"C:\intex.w\Report\Warehouse\" + filename + ".xlsx";

                setUI.Tables["YARN_INVENTORY"].ExportTo(ExportToFormat.Excel2007, filepath);
                System.Diagnostics.Process.Start("EXCEL.EXE", filepath);
                this.Yarn_Inventory.Visibility = Visibility.Visible;

            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }

        private void Weaving_Click(object sender, RoutedEventArgs e)
        {

        }



    }
}
